const nxItem = require('../../behaviors/form/nx-item');
import dataRegexp from '../../utils/tools'
const app = getApp()
Component({
  behaviors: [nxItem],
  properties: {
    // 可设置为任意原生类型, 如 text number idcard digit
    type: {
      type: String
    },
    icon: {
      type: String,
      value: 'icon-a-zu4331'
    },
    needCode: {
      type: Boolean,
      value: true
    }
  },
  data: {
    time: 60,
    showTimer: false
  },
  methods: {
    input: function (e) {
      this.triggerEvent('input', e.detail)
    },
    toGetCode: function () {
      this.triggerEvent('getCode')
      let myreg = /^1[0-9]{10}$/
      if (myreg.test(app.globalData.phone) && app.globalData.phone !== '') {
        this.setData({
          showTimer: true
        })
        let timer = setInterval(() => {
          // 倒计时数字 每秒自减1 
          this.data.time--;
          // 每次自减一 再赋值给它本身
          this.setData({
            time: this.data.time,
          })
          // 判断 显示为0s时 结束定时器 将'重新获取'显示出来 将节流值改为false 将倒计时复原
          if (this.data.time === -1) {
            clearInterval(timer)
            this.setData({
              time: 0,
              showTimer: false
            })
          }
        }, 1000);
      }
    },
  }
})